﻿using Business;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace UI.Areas.Admin.Controllers
{
    public class WebSiteController : Controller
    {
        // GET: Admin/WebSite
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ConfigPayMent()
        {
            return View();
        }

        #region 网站管理Func
        
        public JsonResult Get_ConfigPayMentList()
        {
            PAY_TYPE t_pay_type = new PAY_TYPE();
            List<PAY_TYPE> list = t_pay_type.GetALL();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult Del(int FID)
        {
            httpModel t_httpmodel = new httpModel();
            new PAY_TYPE().Delete(FID);
            t_httpmodel.status = 0;
            t_httpmodel.msg = "删除成功";
            return Json(t_httpmodel, JsonRequestBehavior.AllowGet);
        }
        public JsonResult Edit_ConfigPayMent(PAY_TYPE t_pay_type)
        {
            httpModel t_httpmodel = new httpModel();
            string filename = ProcessUpload();
            if (filename=="文件错误")
            {
                t_httpmodel.msg = "文件错误";
                t_httpmodel.status = DoResult.Failed;
                return Json(t_httpmodel, JsonRequestBehavior.AllowGet);
            }
            if (filename!="")
            {
                t_pay_type.file = "/"+filename;
            }
            if (t_pay_type.ID == 0)
            {
                t_pay_type.Insert(ref t_pay_type);
                t_httpmodel.msg = "添加成功";
            }
            else
            {
                t_pay_type.Update(t_pay_type);
                t_httpmodel.msg = "更新成功";
            }
            t_httpmodel.status = 0;
            return Json(t_httpmodel, JsonRequestBehavior.AllowGet);
        }
        #endregion

        public string ProcessUpload()
        {
            try
            {
                string fileName = Request.Files["choosefile"].FileName;
                if (fileName=="")
                {
                    return "";
                }
                char[] param = { '.' };
                string[] temp = fileName.Split(param);
                string expand = "." + fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
                bool isAllow = false;

                isAllow = (expand == ".txt");//检查文件格式是否合法

                string saveFolder = Request.MapPath("~/");
                if (!isAllow)
                {
                    return "文件错误";
                }

                bool isSaveFolderExists = Directory.Exists(saveFolder);
                if (!isSaveFolderExists)
                {//创建目录
                    Directory.CreateDirectory(saveFolder);
                }
                string newFileSavePath = Path.Combine(saveFolder, fileName); //(Session["userinfo"] as SystemUser).FID + "_" + DateTime.Now.ToString("yyMMddHHmmss") + "cover" + expand;
                 
                Request.Files["choosefile"].SaveAs(newFileSavePath);
                return fileName;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }

        }
    }
}